package web;

import com.alibaba.fastjson.JSON;
import pojo.News;
import pojo.PageBean;
import pojo.User;
import service.Nservice;
import service.Uservice;
import service.impl.NserviceImpl;
import service.impl.UserviceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/news/*")

public class NewServlet extends BaseServlet{
    private Nservice service = new NserviceImpl();
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接受参数，当前页码，每页条数
        String _currentPage=request.getParameter("currentPage");
        String _pageSize =request.getParameter("pageSize");
        //获取查询条件对象
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();
        News news= JSON.parseObject(params, News.class);

        int currentPage=Integer.parseInt(_currentPage);
        int pageSize=Integer.parseInt(_pageSize);
        PageBean pageBean=service.selectByPageAndCondition(currentPage,pageSize,news);
        //2. 存入request域中
        String  jsonString=JSON.toJSONString(pageBean);
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    public void deleteByid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();
        int id= Integer.parseInt(params);
        service.Newsdelete(id);
        response.getWriter().write("success");
    }
    public void deleteByids(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader br = request.getReader();
        String params = br.readLine();
        int[] ids= JSON.parseObject(params,int[].class);
        service.deleteByids(ids);
        response.getWriter().write("success");
    }
}
